//
//  ActivateViewController.m
//  JianChaRiBao
//
//  Created by yuzhou on 13-11-26.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "ActivateViewController.h"
#import "SDImageCache.h"

@interface ActivateViewController ()
{
    IBOutlet UIView *inputView;
    IBOutlet UIView *activatedView;
}
@end

@implementation ActivateViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stateChanged:) name:@"ActivateStateChanged" object:nil];
        [[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(cannotpushtheButton) name:@"cannotpushtheButton" object:nil];
        
    }
    return self;
}


-(void)cannotpushtheButton
{
    
    updataButtonActive.enabled = YES;
    updataButtonInput.enabled = YES;
}




- (void)changeView{
    if ([ActivateManager shareManager].isActivated) {
        activatedView.hidden = NO;
        inputView.hidden = YES;
    }else{
        activatedView.hidden = YES;
        inputView.hidden = NO;
    }

}
- (void)stateChanged:(NSNotification *)noti{
    [self changeView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self changeView];
    NSDictionary *infoDict = [[NSBundle mainBundle]infoDictionary];
    NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
    currentVersionLabel.text = [NSString stringWithFormat:@"当前版本：%@",currentVersion];
    cuttentVersion.text = [NSString stringWithFormat:@"当前版本：%@",currentVersion];
}
- (BOOL)inspectString:(NSString *)text{
    if (text == nil || [text isEqualToString:@""]) {
        ABPromptView * abp = [[ABPromptView alloc]initWithTitle:@"请输入激活码"];
        [abp show];
        [abp release];
        return NO;
    }else if (text.length != 8){
        ABPromptView * abp = [[ABPromptView alloc]initWithTitle:@"验证码非法"];
        [abp show];
        [abp release];
        return NO;
    }else{
        return YES;
    }
    


}
-(void)activateUser{
    [_activateNumTextField resignFirstResponder];
    if ([[[UIDevice currentDevice] systemVersion]floatValue] >= 6.0) {
        if ([self inspectString:_activateNumTextField.text]) {
            [[ActivateManager shareManager] activateWith:_activateNumTextField.text];
        }
    }
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self activateUser];
    [_activateNumTextField resignFirstResponder];
    return YES;
}

- (IBAction)cancelButtonClicled:(id)sender {
    [_activateNumTextField resignFirstResponder];
}
- (IBAction)activateButtonClicked:(id)sender {
    [self activateUser];
}
- (IBAction)deleteCacheButtonClicked:(id)sender {
    NSString *asiDownlaodCachePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"ASIHTTPRequestCache"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:asiDownlaodCachePath]) {
        [[NSFileManager defaultManager] removeItemAtPath:asiDownlaodCachePath error:nil];
    }
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"缓存已清除"] autorelease];
    [promptView show];
}

- (IBAction)updateButtonClicked:(id)sender {
    cuttentVersion.textColor = [UIColor blackColor];
    currentVersionLabel.textColor = [UIColor blackColor];
    
    
    [self updateWithAlert:YES];
}

- (IBAction)updateButtonTouchDown:(id)sender {
    cuttentVersion.textColor = [UIColor darkGrayColor];
    currentVersionLabel.textColor = [UIColor darkGrayColor];
}
- (IBAction)updateButtonTouchUpOutside:(id)sender {
    cuttentVersion.textColor = [UIColor blackColor];
    currentVersionLabel.textColor = [UIColor blackColor];
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//    NSLog(@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:EndTime]);
    [_activateNumTextField resignFirstResponder];
}

-(void)updateWithAlert:(BOOL)alert{
    
    updataButtonInput.enabled = NO;
    updataButtonActive.enabled = NO;
    [[ConvenientTool sharedConvenienTool] ChackAppVersionNeedPopAlert:alert];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [[ConvenientTool sharedConvenienTool] showAppInfo];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [inputView release];
    [activatedView release];
    [cuttentVersion release];
    [_activateNumTextField release];
    [currentVersionLabel release];
    [updataButtonInput release];
    [updataButtonActive release];
    [super dealloc];
}
@end
